//
//  AuthViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-5-17.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AuthViewController.h"

#import "WebAuthorizeViewController.h"

#import "DataManager.h"


@implementation AuthViewController

//@synthesize web;
//
//@synthesize active;
//
//@synthesize auURL;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.title = (NSLocalizedStringFromTable(@"AUTH_USER",@"main",@""));
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBackGetFrob:) name:@"GetFrobFinish" object:nil];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark button_action

-(IBAction)startAuthorize:(id)sender{
	DataManager *dm = [DataManager sharedDataManager];
	// [dm displayID];
	// TODO 对于长时间的联网操作，应该加入进度，或者转菊花来标示
	UIActivityIndicatorView *active = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
	UIBarButtonItem *rb = [[UIBarButtonItem alloc] initWithCustomView:active];
	[active startAnimating];
	[self.navigationItem setRightBarButtonItem:rb];
	
	NSURL *au = [dm getFrobAndAuthURL];
	
	[active stopAnimating];
	[active removeFromSuperview];
	[self.navigationItem setRightBarButtonItem:nil];
	[rb release];
	[active release];
	
	// TODO 针对访问 flickr 出错，或者是联网超时的处理
	if(au){
		[[NSNotificationCenter defaultCenter] postNotificationName:@"GetFrobFinish" object:au];
	}else {
		// TODO 显示 Alert 提示用户联网错误，请检查网络设置
		UIAlertView *alert;
		alert = [[UIAlertView alloc] initWithTitle:(NSLocalizedStringFromTable(@"ERROR",@"main",@"")) message:(NSLocalizedStringFromTable(@"AUTH_ERROR",@"main",@"")) delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}

-(IBAction)createFlickrAccount:(id)sender{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"GetFrobFinish" object:[NSURL URLWithString:@"http://m.flickr.com"]];
}

#pragma mark notifycation_worker
-(void)callBackGetFrob:(id)notification{
#ifdef debug
	NSLog(@"GetFrobFinish ok");
#endif
	if([notification isKindOfClass:[NSNotification class]]){
		NSNotification *n = notification;
		id obj = [n object];
		if([obj isKindOfClass:[NSURL class]]){
			NSURL *url = obj;
			WebAuthorizeViewController *wac = [[WebAuthorizeViewController alloc] initWithNibName:@"WebAuthorizeViewController" bundle:nil];
			wac.url = [url copy];
			[self.navigationController pushViewController:wac animated:YES];
			[wac release];
		}
	}
}


- (void)dealloc {
    [super dealloc];
}


@end
